
from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
import serial
import serial.tools.list_ports
import time
import numpy as np
import threading

channel1_data = [1,2,3,4,5,6,7,8,9,10]
channel2_data = [10,20,30,40,50,60,70,80,90,100]
channel3_data = [100,200,300,400,500,600,700,800,900,1000]

def ui_custom_setup(ui):
    ports = list(serial.tools.list_ports.comports())

    ui.plot_widget = pg.GraphicsLayoutWidget(show=True, title="Scatter Plot Symbols") #pg.PlotWidget()
    ui.horizontalLayout_2.addWidget(ui.plot_widget)
    pg.setConfigOptions(antialias=True) # 使曲线看起来更光滑，而不是锯齿状
    ui.plot_widget.setBackground('#f0f0f0')
    #ui.plot_widget.drawForeground('blue')
    
    #ui.plot_widget.setLabel('left', 'DATA')
    #ui.plot_widget.setLabel('bottom', 'TIME(ms)')
    #ui.plot_widget.showGrid(x=True, y=True)

def ui_custom_thread(ui):
    while True:
        # 更新两个通道的数据
        print('updata data')
        time.sleep(1)

# 创建刷图线程
def creat_ui_custom_task(ui):
    print("3.创建实时刷图线程")

    channel1_data = np.sin(np.linspace(0, 100, 100))
    channel2_data = np.cos(np.linspace(0, 100, 100))
    channel3_data = np.cos(np.linspace(0, 100, 100)*2)

    # 在窗口中添加两个PlotWidget，分别显示两个通道的波形
    p1 = ui.plot_widget.addPlot(title='DATA')
    p1.plot(channel1_data, pen=(255,0,0), name="symbol='r'")
    p1.plot(channel2_data, pen=(0,128,0), name="symbol='g'")
    p1.plot(channel3_data, pen=(19,234,201), name="symbol='b'")
    p1.setXRange(-2, 4)
    #p1.grid(True,linestyle="--")
    p1.showGrid(x=True, y=True) # 显示网格

    thread_recv = threading.Thread(target=ui_custom_thread, args=[ui])
    thread_recv.daemon = True
    thread_recv.start()
